home *** CD-ROM | disk | FTP | other *** search
/ Packard Bell - Internet on a CD / internet on a cd.cdr / Internet / sites / Clementine_NASA / clemdsrc.hqx / bitstrm.c next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  4.1 KB  |  156 lines

  1. /*
  2.  *    THIS ROUTINE IS PART OF THE CLEMENTINE PDS FILE READER PROGRAM.
  3.  *    IT WAS WRITTEN BY ACT CORP. IN DIRECT SUPPORT TO THE 
  4.  *    CLEMENTINE (DSPSE) PROGRAM.
  5.  *
  6.  *    IF YOU FIND A PROBLEM OR MAKE ANY CHANGES TO THIS CODE PLEASE CONTACT
  7.  *    Dr. Erick Malaret at ACT Corp. 
  8.  *            tel: (703) 742-0294
  9.  *                 (703) 683-7431
  10.  *                       email:    nrlvax.nrl.navy.mil
  11.  *
  12.  *    Changes were made for the Macintosh version.  These changes were
  13.  *    made by Tracie Sucharski at the USGS in Flagstaff and are marked with
  14.  *    a comment denoted as  " TS ".  Macintosh does not allow an application to
  15.  *    use printf.  A routine, "errmsg" was substituted which writes error 
  16.  *    messages to a window.
  17.  *
  18.  *
  19.  */
  20. #include <stdio.h>
  21. #include "jpeg_c.h"
  22.  
  23. extern long int resnum;   /* TS */
  24. extern long int resnumq;  /* TS */
  25. void errmsg();
  26.  
  27. void cBitStream( BitStream *bs, char *fn, Fmode fm )
  28. {
  29.     cByteStream( &bs->bytestream, fn, fm );
  30.     bs->BitBuffer = 0;
  31.     bs->bytesout = 0;
  32.     bs->outstring = NULL;
  33.     bs->BitBuffMask =  (fm==OUTPUT) ? 0x80:0x00;
  34.     bs->bitmask[0] = 0x0000; bs->bitmask[1] = 0x0001; bs->bitmask[2] = 0x0002;
  35.     bs->bitmask[3] = 0x0004; bs->bitmask[4] = 0x0008; bs->bitmask[5] = 0x0010;
  36.     bs->bitmask[6] = 0x0020; bs->bitmask[7] = 0x0040; bs->bitmask[8] = 0x0080;
  37.     bs->bitmask[9] = 0x0100; bs->bitmask[10] = 0x0200; bs->bitmask[11] = 0x0400;
  38.     bs->bitmask[12] = 0x0800; bs->bitmask[13] = 0x1000; bs->bitmask[14] = 0x2000;
  39.     bs->bitmask[15] = 0x4000; bs->bitmask[16] = 0x8000;
  40. }
  41.  
  42. void dBitStream(BitStream *bs)
  43. {
  44.     if ( bs->bytestream.mode == OUTPUT ) {
  45.         if ( bs->BitBuffMask != 0x80 ) {
  46.             while (bs->BitBuffMask) {
  47.                 bs->BitBuffer |= bs->BitBuffMask;
  48.                 bs->BitBuffMask >>= 1;
  49.             }
  50.             if ( bs->mode )
  51.                 bs->outstring[bs->bytesout] = bs->BitBuffer;
  52.             else
  53.                 ByteStream_write( &bs->bytestream, bs->BitBuffer );
  54.             bs->bytesout++;
  55.         }
  56.         if ( bs->mode ) {
  57.             if ( fwrite(bs->outstring,sizeof(char),bs->bytesout,bs->bytestream.file) == 0 )
  58.                 errmsg("Error: writing output bitstream to file.\n",&resnum);  /* TS */
  59.         }
  60.         if ( fseek(bs->bytestream.file,8,0) )
  61.             errmsg("Error: fseek in subroutine dBitStream().\n",&resnum);  /* TS */
  62.         else {
  63.             if ( fwrite(&(bs->bytesout),sizeof(long),1,bs->bytestream.file) == 0 )
  64.                 errmsg("Error: writing bytesout value to file.\n",&resnum);  /* TS */
  65.         }
  66.     }
  67.     dByteStream(&bs->bytestream);
  68. }
  69.  
  70. short BitStream_write(BitStream *bs, short bits, short width)
  71. {
  72.     unsigned short BitMask = bs->bitmask[width];
  73.  
  74.     while ( BitMask ) {
  75.         if ( bits & BitMask ) bs->BitBuffer |= (short)bs->BitBuffMask;
  76.         BitMask >>= 1;
  77.         bs->BitBuffMask >>= 1;
  78.         if ( !bs->BitBuffMask ) {
  79.             if ( bs->mode )
  80.                 bs->outstring[bs->bytesout] = bs->BitBuffer;
  81.             else
  82.                 ByteStream_write( &bs->bytestream, bs->BitBuffer );
  83.             bs->bytesout++;
  84.             bs->BitBuffer = 0;
  85.             bs->BitBuffMask = 0x80;
  86.         }
  87.     }
  88.     return bs->bytestream.stat;
  89. }
  90.  
  91. short BitStream_read( BitStream *bs, short w )
  92. {
  93.     unsigned short RetVal = 0, BitMask = bs->bitmask[w];
  94.  
  95.     while ( BitMask ) {
  96.         if ( !bs->BitBuffMask ) {
  97.             if ( bs->mode ) {
  98.                 bs->BitBuffer = ((short)bs->outstring[bs->bytesout]) & 0x00ff;
  99.             } else
  100.                 bs->BitBuffer = ByteStream_read(&bs->bytestream);
  101.  
  102.             bs->bytesout++;
  103.             bs->BitBuffMask = 0x80;
  104.         }
  105.         if ( bs->BitBuffer & bs->BitBuffMask ) RetVal |= BitMask;
  106.         bs->BitBuffMask >>= 1;
  107.         BitMask >>= 1;
  108.     }
  109.     return RetVal;
  110. }
  111.  
  112. void cByteStream(ByteStream *Bs, char *FileName, Fmode FileMode)
  113. {
  114.     Bs->mode = FileMode;
  115.     if ( FileName != NULL ) {
  116.         Bs->file = fopen(FileName,(Bs->mode==INPUT) ? "rb":"wb");
  117.         if ( Bs->file == NULL ) errmsg("ByteStream constructor error.\n",&resnum);  /* TS */
  118.         Bs->stat = 0;
  119.     }
  120. }
  121.  
  122. void dByteStream(ByteStream *Bs)
  123. {
  124.     if ( Bs->file ) {
  125.         fclose(Bs->file);
  126.         Bs->file = NULL;
  127.     }
  128. }
  129.  
  130. short ByteStream_read(ByteStream *Bs)
  131. {
  132.     short     c, d;
  133.     char    cval;
  134.         int    n;
  135.  
  136.     if ( Bs->mode == INPUT ) {
  137.         /* c = fgetc( Bs->file ); */
  138.         n=fread(&cval,sizeof(char),1,Bs->file);
  139.                 c    = cval; 
  140.         if ( n != 1 ) Bs->stat = EOF;
  141.         return c;
  142.     } else
  143.         Bs->stat = EOF;
  144. }
  145.  
  146. short ByteStream_write(ByteStream *Bs, short c)
  147. {
  148.     if ( (Bs->mode != OUTPUT) || (fputc(c,Bs->file) == EOF) ) Bs->stat = EOF;
  149.     return Bs->stat;
  150. }
  151.  
  152. short ByteStream_status(ByteStream *Bs)
  153. {
  154.     return Bs->stat;
  155. }
  156.